﻿<UserControl x:Class="CredentialsManager.Views.ManagementView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:ct="clr-namespace:CredentialsManager.Controls"
             xmlns:wi="clr-namespace:CredentialsManager.Controls.WorkingImage" 
             xmlns:vw="clr-namespace:CredentialsManager.Views"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style TargetType="{x:Type Button}" x:Key="AssignButton">
            <Setter Property="Margin" Value="0, 0, 0, 10" />
            <Setter Property="Padding" Value="5, 5, 5, 5" />
        </Style>
        <Style x:Key="AssignSelectBothButton" TargetType="{x:Type Button}" BasedOn="{StaticResource AssignButton}">
            <Setter Property="IsEnabled" Value="True" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding UserListViewModel}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding RoleListViewModel}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding UserListViewModel.SelectedUsers}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding RoleListViewModel.SelectedRoles}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding UserListViewModel.SelectedUsers.Count}" Value="0">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding RoleListViewModel.SelectedRoles.Count}" Value="0">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid Panel.ZIndex="2"
                HorizontalAlignment="Center" 
                VerticalAlignment="Center">
            <Grid.Style>
                <Style TargetType="{x:Type Grid}" BasedOn="{StaticResource HideIfNotLoadingStyle}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding UserListViewModel.IsLoading}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RoleListViewModel.IsLoading}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ApplicationListViewModel.IsLoading}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <wi:WorkingImage />
        </Grid>
        <DockPanel LastChildFill="True" Panel.ZIndex="1">
            <ContentControl 
                DockPanel.Dock="Top"
                Content="{Binding}" 
                DataContext="{Binding ApplicationListViewModel}" />
            <Grid DockPanel.Dock="Bottom" Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Credentials service location: " Grid.Column="0" />
                <TextBlock Text="{Binding CredentialsServiceLocation}" Grid.Column="1" />
            </Grid>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <ContentControl Content="{Binding}" Grid.Column="0" Margin="10" DataContext="{Binding UserListViewModel}" />
                <StackPanel VerticalAlignment="Center" Grid.Column="1" Margin="10">
                    <StackPanel.Style>
                        <Style TargetType="{x:Type StackPanel}">
                            <Setter Property="Visibility" Value="Visible" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RoleListViewModel}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding UserListViewModel}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <Button Content="_Assign" Style="{StaticResource AssignSelectBothButton}"
                            Command="{Binding AssignUsersToRolesCommand}">
                        <Button.ToolTip>
                            <ct:TitledTooltip Title="Assign" BodyText="Assigns all the selected users to all the selected roles" />
                        </Button.ToolTip>
                    </Button>
                    <Button Content="Remo_ve" Style="{StaticResource AssignSelectBothButton}"
                            Command="{Binding RemoveUsersFromRolesCommand}">
                        <Button.ToolTip>
                            <ct:TitledTooltip Title="Remove" BodyText="Removes the selected users from the selected roles" />
                        </Button.ToolTip>
                    </Button>
                    <Button Content="Remove _roles" Command="{Binding RemoveAllRolesFromUsersCommand}">
                        <Button.Style>
                            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource AssignButton}">
                                <Setter Property="IsEnabled" Value="True" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding UserListViewModel}" Value="{x:Null}">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding UserListViewModel.SelectedUsers}" Value="{x:Null}">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding UserListViewModel.SelectedUsers.Count}" Value="0">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                        <Button.ToolTip>
                            <ct:TitledTooltip Title="Remove roles" BodyText="Remove all roles for selected users" />
                        </Button.ToolTip>
                    </Button>
                    <Button Content="Remove _users" Command="{Binding RemoveAllUsersFromRolesCommand}">
                        <Button.Style>
                            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource AssignButton}">
                                <Setter Property="IsEnabled" Value="True" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RoleListViewModel}" Value="{x:Null}">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding RoleListViewModel.SelectedRoles}" Value="{x:Null}">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding RoleListViewModel.SelectedRoles.Count}" Value="0">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                        <Button.ToolTip>
                            <ct:TitledTooltip Title="Remove users" BodyText="Remove all users from selected roles" />
                        </Button.ToolTip>
                    </Button>
                </StackPanel>
                <ContentControl Grid.Column="2" Margin="10" Content="{Binding RoleListViewModel}" />
                <Grid Grid.Column="3">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <vw:UsersInRoleView DataContext="{Binding UsersInRoleViewModel}" Grid.Row="0" Margin="10" />
                    <vw:RolesForUserView DataContext="{Binding RolesForUserViewModel}" Grid.Row="1" Margin="10" />
                </Grid>
            </Grid>
        </DockPanel>
    </Grid>
</UserControl>
